package com.nuaa.shopping.dao.impl;


import java.util.List;

import com.nuaa.shopping.dao.UserDao;
import com.nuaa.shopping.entity.User;
import com.nuaa.shopping.mapper.UserMapper;
import com.nuaa.shopping.util.JdbcTemplate;
import com.nuaa.shopping.util.RowMapper;

public class UserDaoImpl implements UserDao{
	
	private RowMapper<User> rm = new UserMapper();

	@Override
	public void insert(User user) {
		String sql = new StringBuffer()
			.append(" insert into ")
			.append(" 	t_user ")
			.append(" 		(username,password,phone,address,status) ")
			.append(" values ")
			.append(" 		(?,?,?,?,?) ")
			.toString();
		JdbcTemplate.update(sql, user.getUsername(),user.getPassword(),user.getPhone(),user.getAddress(),user.getStatus());
	}

	@Override
	public User selectByUsername(String username) {
		String sql = new StringBuffer()
			.append(" select ")
			.append(" 	* ")
			.append(" from ")
			.append(" 	t_user ")
			.append(" where ")
			.append(" 	username=? ")
			.toString();
		return JdbcTemplate.queryForObject(sql, rm, username);
	}

	@Override
	public User selectByUsernameAndPassword(User user) {
		String sql = new StringBuffer()
		.append(" select ")
		.append(" 	* ")
		.append(" from ")
		.append(" 	t_user ")
		.append(" where ")
		.append(" 	username=? ")
		.append(" 		and ")
		.append(" 	password=? ")
		.toString();
		return JdbcTemplate.queryForObject(sql, rm, user.getUsername(),user.getPassword());
	}

}
